# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.60.0
_boostver=${pkgver//./_}
pkgrel=4
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64)"
arch=('any')
url="https://www.boost.org/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3")
options=('strip' 'buildflags' 'staticlibs')
source=(https://downloads.sourceforge.net/sourceforge/boost/boost_${_boostver}.tar.bz2
        boost-1.48.0-add-bjam-man-page.patch
        boost-1.50.0-fix-non-utf8-files.patch
        boost-1.58.0-pool.patch
        boost-1.54.0-bootstrap.patch
        boost-1.55.0-python-test-PyImport_AppendInittab.patch
        boost-1.57.0-build-optflags.patch
        boost-1.57.0-mpl-print.patch
        boost-1.58.0-pool-test_linking.patch
        boost-1.57.0-python-abi_letters.patch
        boost-1.57.0-python-libpython_dep.patch
        boost-1.57.0-spirit-unused_typedef.patch
        boost-1.58.0-address-model.patch
        boost-1.60.0-mingw-context.patch
        boost-include-intrin-h-on-mingw-w64.patch
        using-mingw-w64-python.patch
        msys2-mingw-folders-bootstrap.patch
        boost-1.60.0-shared_ptr_registration.patch)
sha256sums=('686affff989ac2488f79a97b9479efb9f2abae035b5ed4d8226de6857933fd3b'
            'c5eae6354693c6b4e6364d4cf6f06c6c3de4c28486bf812cbd291f4410126cf8'
            'f32d189cc661e9b843cf0a3c1528de41aaf4f1ea69d6150b5bcd0a2525d36424'
            '4551ba9edf64c8ccdab4f4890c20e2e8cf6d4ffa1169d251df11e30d25b886b8'
            '68bee1f0cfe3073ef4414d415566dbded678611f6b3aacc0bd99d1fffcdfe084'
            '16dc2c42d6e509299133ca6d2aa3f87678f8904a630160ea93aebf992066d31c'
            'a9a95bdc25a1449c542c667c95bd128ca8a19c0f6282c53671137b2d1b74b0f4'
            'd4be505bf6cd08b9da232d0311bfda2820dd75cf8911ed07b72eddc418c73614'
            'b15743957aa1c7eb1e861dfe96247e50b7afd96258d960be403851ce62923c87'
            'cf46e9b791b45937d5af26b0f35dcdfa416cd9b62ef6c29e3064464835634628'
            '01758929643f92530512230d37df9793e6481cd6ce6310e3a79cee5ba287858c'
            '4768e576f8dcb57e1a7d7dcf9922659dd386c4974142155f23aff0b508a2117d'
            '6182b9cf28e069920a5e7bf9b7d4155df196fc6ee6a31385aaf17c36a91d55c7'
            'cb4b080b68f2d1176c7c9e6302dd9263b8a81e3226b323745765f73c3dfaba6f'
            '9a269375b8eb262b93fb18ea6b2ac8c97b16fb71b8bab900faa4c2df2816f2eb'
            '0dd6346d369850aad13bf8d9bc2f0abc16d4b0586e34d61fab11f65d3d7fa9d4'
            'ff22cad312b49625cc54edf41b210f2fc697322403e074792c82b0b5f45c4060'
            'd673f2df169c5a86dda8036a34da1a3f5c5787a25009d7f45c8b4c3615fd0950')

prepare() {
  cd "${srcdir}/boost_${_boostver}"

  patch -p1 -i ${srcdir}/boost-1.54.0-bootstrap.patch

  # https://svn.boost.org/trac/boost/ticket/6150
  patch -p1 -i ${srcdir}/boost-1.50.0-fix-non-utf8-files.patch

  # Add a manual page for bjam, based on the on-line documentation:
  # http://www.boost.org/boost-build2/doc/html/bbv2/overview.html
  rm tools/build/v2/doc/bjam.1 | true
  patch -p1 -i ${srcdir}/boost-1.48.0-add-bjam-man-page.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=828856
  # https://bugzilla.redhat.com/show_bug.cgi?id=828857
  # https://svn.boost.org/trac/boost/ticket/6701
  patch -p0 -i ${srcdir}/boost-1.58.0-pool.patch

  # https://svn.boost.org/trac/boost/ticket/5637
  patch -p1 -i ${srcdir}/boost-1.57.0-mpl-print.patch

  # https://svn.boost.org/trac/boost/ticket/8870
  patch -p1 -i ${srcdir}/boost-1.57.0-spirit-unused_typedef.patch

  # https://svn.boost.org/trac/boost/ticket/9038
  patch -p1 -i ${srcdir}/boost-1.58.0-pool-test_linking.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1102667
  patch -p1 -i ${srcdir}/boost-1.57.0-python-libpython_dep.patch
  patch -p1 -i ${srcdir}/boost-1.57.0-python-abi_letters.patch
  patch -p1 -i ${srcdir}/boost-1.55.0-python-test-PyImport_AppendInittab.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1190039
  patch -p1 -i ${srcdir}/boost-1.57.0-build-optflags.patch

  # Prevent gcc.jam from setting -m32 or -m64.
  patch -p1 -i ${srcdir}/boost-1.58.0-address-model.patch

  # https://svn.boost.org/trac/boost/ticket/7262
  patch -p1 -i ${srcdir}/boost-1.60.0-mingw-context.patch

  # Fix FTBFS on recent mingw-w64 and also use intrinsics based
  # versions of the Interlocked symbols which are better optimized
  patch -p0 -i ${srcdir}/boost-include-intrin-h-on-mingw-w64.patch

  # Use out own build Python
  patch -p1 -i ${srcdir}/using-mingw-w64-python.patch

  patch -p1 -i ${srcdir}/msys2-mingw-folders-bootstrap.patch
  
  # https://github.com/boostorg/python/issues/56
  patch -p2 -i ${srcdir}/boost-1.60.0-shared_ptr_registration.patch
}

build() {
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  lndir "${srcdir}/boost_${_boostver}" "${srcdir}/build-${MINGW_CHOST}"

  cd "${srcdir}/build-${MINGW_CHOST}"

  echo "using python : 2.7 : ${PREFIX_DEPS}/bin/python2.exe : ${PREFIX_DEPS}/include/python2.7 ;" >> ./tools/build/src/user-config.jam
  echo "using python : 3.5 : ${PREFIX_DEPS}/bin/python3.exe : ${PREFIX_DEPS}/include/python3.5m : ${PREFIX_DEPS}/lib/python3.5/config-3.5m ;" >> ./tools/build/src/user-config.jam
  #echo "using gcc : : ${PREFIX_DEPS}/bin/g++.exe : <rc>${PREFIX_DEPS}/bin/windres.exe ;" >> ./tools/build/src/user-config.jam

  ./bootstrap.sh --with-icu=${MINGW_PREFIX} --with-python=${MINGW_PREFIX} --with-toolset=gcc

  case ${CARCH} in
    i686)
      local _model=32
    ;;
    x86_64)
      local _model=64
    ;;
  esac

  if check_option "debug" "y"; then
      local variant=debug
  else
      local variant=release
  fi

  ./b2 \
    toolset=gcc \
    link=shared,static \
    address-model=${_model} \
    pch=off \
    runtime-link=shared \
    threading=multi \
    threadapi=win32 \
    variant=${variant} \
    --debug-configuration \
    --prefix=${pkgdir}${MINGW_PREFIX} \
    --layout=tagged \
    --without-mpi \
    -sHAVE_ICU=1 \
    -sICU_PATH=${MINGW_PREFIX} \
    -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
    -sICONV_PATH=${MINGW_PREFIX} \
    -sICONV_LINK="-L${MINGW_PREFIX}/lib -liconv" \
    -sNO_BZIP2 \
    -sBZIP2_BINARY=bz2 \
    -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
    -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
    -sNO_ZLIB \
    -sZLIB_BINARY=z \
    -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
    -sZLIB_LIBPATH=${MINGW_PREFIX}/zlib \
    -d2
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  case ${CARCH} in
    i686)
      local _model=32
    ;;
    x86_64)
      local _model=64
    ;;
  esac

  if check_option "debug" "y"; then
      local variant=debug
  else
      local variant=release
  fi

  ./b2 \
      toolset=gcc \
      variant=${variant} \
      threading=multi \
      threadapi=win32 \
      link=shared,static \
      runtime-link=shared \
      --prefix=${pkgdir}${MINGW_PREFIX} \
      -sHAVE_ICU=1 \
      -sICU_PATH=${MINGW_PREFIX} \
      -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
      -sICONV_PATH=${MINGW_PREFIX} \
      -sICONV_LINK="-L${MINGW_PREFIX}/lib -liconv" \
      -sNO_BZIP2 \
      -sBZIP2_BINARY=bz2 \
      -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
      -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
      -sNO_ZLIB \
      -sZLIB_BINARY=z \
      -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
      -sZLIB_LIBPATH=${MINGW_PREFIX}/zlib \
      address-model=${_model} \
      --layout=tagged install \
      --without-mpi

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin
}
